package com.lizard.schedule.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.TextView;

import com.lizard.schedule.R;

/**
 * schedule自定义风格ProgressDialog
 */
public class ScheduleProgressDialog extends Dialog {

    private Builder mBuilder;

    ScheduleProgressDialog(Context context) {
        super(context, R.style.ScheduleDialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_progress_dialog);
        initTextTv();
        initProgressRing();
    }

    /**
     * 初始化进度环和旋转动画
     */
    private void initProgressRing(){
        RotateAnimation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setRepeatCount(-1);
        anim.setDuration(1000);
        anim.setInterpolator(new LinearInterpolator());

        View progressRing = findViewById(R.id.progress_ring);
        progressRing.startAnimation(anim);
    }

    /**
     * 文本内容初始化
     */
    private void initTextTv(){
        TextView textTv = ((TextView) findViewById(R.id.text));
        if(TextUtils.isEmpty(mBuilder.getText())){
            textTv.setVisibility(View.GONE);
        } else{
            textTv.setVisibility(View.VISIBLE);
            textTv.setText(mBuilder.getText());
        }
    }

    public static class Builder{

        /**
         * ProgressDialog显示的文案
         */
        private String mText;

        private Context mContext;

        private boolean mCancelable = true;

        private ScheduleProgressDialog mScheduleProgressDialog;

        public Builder(Context context){
            mContext = context;
        }

        /**
         * 设置文本
         *
         * @param textId
         */
        public Builder setText(int textId){
            return setText(mContext.getResources().getString(textId));
        }

        /**
         * 设置文本
         *
         * @param text
         */
        public Builder setText(String text){
            mText = text;
            return this;
        }

        public String getText(){
            return mText;
        }

        public Builder setCancelable(boolean cancelable){
            mCancelable = cancelable;
            return this;
        }

        public void cancel(){
            if(mScheduleProgressDialog != null){
                mScheduleProgressDialog.cancel();
            }
        }

        /**
         * 显示对话框
         */
        public void show(){
            mScheduleProgressDialog = new ScheduleProgressDialog(mContext);
            mScheduleProgressDialog.mBuilder = this;
            mScheduleProgressDialog.setCancelable(mCancelable);
            mScheduleProgressDialog.show();
        }
    }
}
